/*
Same idea as usual entry events
- BUT take list of first users, split based on study date
- Track market shares over time
- January 17th, 2008 article in WSJ 91/16 stock market response)
*/



clear
use user_first_cholesterol_dates


gen rel_date = first_use_date - mdy(1,16,2008)

gen treat = 1 if rel_date > 0 & rel_date <= 180
replace treat = 0 if rel_date <= 0 & rel_date > - 180

drop if treat == .

tab treat

save user_treatment_vytorin, replace



clear
use user_quarter_choice_cholesterol


// focus on cholesterol
tab indic_id
keep if indic_id == 5


merge m:1 enrolid using "user_treatment_vytorin", keep(3)
drop _merge

merge m:1 generic_id using "generic_name_mapping"
keep if _merge == 3
drop _merge


// set up the data for analysis

egen first_q = min(quarter), by(enrolid)
gen first_choice_temp = generic_id if quarter == first_q
egen first_choice = max(first_choice_temp), by(enrolid)

gen first_generic_temp = generic if quarter == first_q
egen first_generic = max(first_generic_temp), by(enrolid)

gen first_xr_temp = xr if quarter == first_q
egen first_xr = max(first_xr_temp), by(enrolid)


gen period = quarter - first_q
gen chose_initial = (generic_id == first_choice) & (generic == first_generic) & (xr == first_xr)

gen chose_vytorin = (gennme == "Ezetimibe/Simvastatin")

// Does this need correcting?
gen initial = chose_vytorin if quarter == first_q  // rel_quart == 0 (old code; this is probably cleaner; use their first choice, otherwise some not there in period 0)
egen initial_choice = max(initial), by(enrolid)


gen rel_quart = quarter - qofd(mdy(1,16,2008))



////////////
// GRAPHS //
////////////


// graph to illustrate difference
collapse (mean) chose_vytorin, by(treat rel_quart)

twoway (line chose_vytorin rel_quart if treat == 1 & rel_quart > -5 & rel_quart <= 20) ///
(line chose_vytorin rel_quart if treat == 0 & rel_quart > -5 & rel_quart <= 20), ///
xtitle("Quarter Relative to Study Publication") ytitle("Fraction on New Drug") legend(label(1 "Started After (Treated)") label(2 "Started Before (Control)"))

graph export "event_study_vytorin_study.pdf", replace
graph save "event_study_vytorin_study", replace





